package day21;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class TestPipedStream {
	public static void main(String[] args) throws Exception{
		PipedInputStream pis = new PipedInputStream();
		PipedOutputStream pos = new PipedOutputStream();
		pis.connect(pos);
		Thread t1 = new InputThread(pis);
		Thread t2 = new OutputThread(pos);
		t1.start();
		t2.start();
	}
}
class InputThread extends Thread{
	private DataInputStream dis;
	public InputThread(PipedInputStream pis){
		BufferedInputStream bis = new BufferedInputStream(pis);
		dis = new DataInputStream(bis);
	}
	public void run(){
		try {
			for(int i=0; i<100; i++){
				int data = dis.readInt();
				System.out.println(data);
				sleep(100);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
class OutputThread extends Thread{
	private DataOutputStream dos;
	public OutputThread(PipedOutputStream pos){
		BufferedOutputStream bos = new BufferedOutputStream(pos);
		dos = new DataOutputStream(bos);
	}
	public void run(){
		try {
			for(int i=0; i<100; i++){
				dos.writeInt(i);
				dos.flush();
				sleep(100);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}